package com.sid.oncf.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.sid.oncf.dao.interfaces.IVoyageDao;
import com.sid.oncf.entity.Voyages;

public class VoyageDaoImpl implements IVoyageDao {

	@PersistenceContext
	private EntityManager em;
	
	public boolean save(Voyages v) {
		em.persist(v);
		return true;
	}

	public boolean update(Voyages v) {
		em.merge(v);
		return true;
	}

	public boolean delete(int id) {
		em.remove(find(id));
		return true;
	}

	public Voyages find(int id) {
		return em.find(Voyages.class,id);
	}

	public List<Voyages> findAll() {
		Query query=em.createQuery("select v from Voyages v",Voyages.class);
		return query.getResultList();
	}

	public List<Voyages> recherche(int gareDepart, int gareArrivee) {
		Query query=em.createQuery("select v from Voyages v where v.garesByIdGareDepart.idGare=:idGareDepart "
				                    + "and v.garesByIdGareArrivee.idGare=:idGareArrivee");
		query.setParameter("idGareDepart", gareDepart);
		query.setParameter("idGareArrivee", gareArrivee);
		return query.getResultList();
	}

}
